<?php
/*
	商品独立佣金设置
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
$tabs=array(
	array('op'=>'list','title'=>'绩效商品列表'),
	array('op'=>'edit','title'=>'添加/编辑商品'),
);
set_html('tabs',$tabs);
if($_RQ['op']=='list'){
	$params=array('uniacid'=>$_SESSION['uniacid']);
	if($_RQ['goodsid']){
		$params['goodsid']=intval($_RQ['goodsid']);
		$return['params']['goodsid']=$_RQ['goodsid'];
	}
	$psize=15;
	$pindex=intval($_RQ['page'])?:1;
	$parameter=array(
		'params'=>$params,
		'limit'=>array($pindex,$psize),
		'orderby'=>'id desc',
	);
	$list=$this->get_list('goods',$parameter,$total);
	if($list){
		foreach($list as &$item){
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'list'=>array('data'=>'list','tip'=>'【温馨提示】：','list'=>array(
			array('name'=>'title','title'=>'商品名称','type'=>'string'),
			array('name'=>'image','title'=>'商品图片','type'=>'image'),
			array('name'=>'pid','title'=>'一级分类','type'=>'select2','options'=>'groups'),
			array('name'=>'cid','title'=>'二级分类','type'=>'select2','options'=>'groups'),
			array('name'=>'price','title'=>'商品价格','type'=>'string'),
			array('name'=>'benprice','title'=>'成本价格','type'=>'string'),
			array('name'=>'status','title'=>'状态','type'=>'switch','set'=>'set'),
		)),
		'search'=>array(
			array('name'=>'title','width'=>15,'title'=>'商品名称','type'=>'string'),
			array('name'=>'status','width'=>15,'title'=>'状态','type'=>'radio','options'=>'status'),
		),
	);
	$return['groups']=pdo_getall('jy_yxgz_goods_group',array('uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='edit'){
	if($_RQ['postdata']){
		$post=get_postdata();
		$data=array(
			'goodsid'=>intval($post['goodsid']),
			'pid'=>intval($post['pid']),
			'cid'=>intval($post['cid']),
			'status'=>intval($post['status']),
			'title'=>$post['title'],
			'image'=>$post['image'],
			'price'=>$post['price'],
			'benprice'=>$post['benprice'],
		);
		$id=intval($post['id']);
		if($id){
			$this->update('goods',$data,array('id'=>$id));
		}else{
			$data['uniacid']=$_SESSION['uniacid'];
			$data['createtime']=time();
			$this->insert('goods',$data);
		}
		exi('设置成功！','','list');
	}
	$id=intval($_RQ['id']);
	if($id){
		$item=$this->get('goods',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}else{
		$item=array(
			'title'=>'',
		);
	}
    $return['item']=$item;
	$htmls=array(
		'type'=>'edit',
		'desc'=>'商品设置',
		'edit'=>array(
			array('name'=>'title','title'=>'商品名称','type'=>'string'),
			array('name'=>'image','title'=>'商品图片','type'=>'image'),
			array('type'=>'select2_line','name'=>'pid','options'=>'groups','title'=>'一级分类',
				'name2'=>'cid','options2'=>'groups2','title2'=>'二级分类',
			),
			array('name'=>'price','title'=>'商品价格','type'=>'string'),
			array('name'=>'benprice','title'=>'成本价格','type'=>'string'),
			array('name'=>'goodsid','title'=>'关联商品','type'=>'choose','action'=>'jy_weishop/goods.get_goods/'),
			array('name'=>'status','title'=>'状态','type'=>'switch'),
		),
	);
	$return['groups']=pdo_getall('jy_yxgz_goods_group',array('uniacid'=>$_SESSION['uniacid'],'pid'=>0),array('id','title'),'id');
	foreach($return['groups'] as &$pv){
		$pv['groups2']=pdo_getall('jy_yxgz_goods_group',array('uniacid'=>$_SESSION['uniacid'],'pid'=>$pv['id']),array('title','id'),'id');
	}
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='del'){
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
	}else{
		$del=array();
	}
	if($_RQ['id']){
		$del[]=$_RQ['id'];
	}
	foreach($del as $id){
		$id=intval($id);
		if($id){
			$this->delete('goods',array('id'=>$id));
		}
	}
	exi('删除成功！');
}
if($_RQ['op']=='set'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('参数错误','error');
	}
	$post=get_postdata();
	if(isset($post['status'])){
		$this->update('goods',array('status'=>$post['status']),array('id'=>$id));
	
	}
	exi('操作成功！');
}